/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/

package cn.com.dhc.danlu.widget.home;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import cn.com.dhc.danlu.R;

/**
 * @author: 李一方
 * @version: 1.0.0
 * @since: 2016-08-04 11:37
 * @mail: liyifang@danlu.com
 * @desc: 带有左右两个角标的图片视图
 */
public class CornerImageView extends FrameLayout {
    private SimpleDraweeView ivMain;
    private ImageView ivTopLeft;
    private ImageView ivTopRight;

    public CornerImageView (Context context) {
        super (context);

        init ();
    }

    private void init () {
        View rootView = LayoutInflater.from (getContext ()).inflate (R.layout.dl_item_home_goods_pic, this, true);
        ivMain = (SimpleDraweeView) rootView.findViewById (R.id.iv_main);
        ivTopLeft = (ImageView) rootView.findViewById (R.id.iv_top_left);
        ivTopRight = (ImageView) rootView.findViewById (R.id.iv_top_right);

        setBackgroundColor (getResources ().getColor (R.color.transparent));
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder (getResources ());
        GenericDraweeHierarchy hierarchy = builder.setFadeDuration (300).setPlaceholderImage (getResources ().getDrawable (R.drawable.ic_default)).setFailureImage (getResources ().getDrawable (R.drawable.ic_default)).setActualImageScaleType (ScalingUtils.ScaleType.FIT_XY).build ();
        ivMain.setHierarchy (hierarchy);
    }

    public CornerImageView (Context context, AttributeSet attrs) {
        super (context, attrs);

        init ();
    }

    public void loadImg (String url) {
        if (TextUtils.isEmpty (url)) {
            //设置一个占位url
            url = " http://asset.danlu.com/";
        }
        ivMain.setImageURI (Uri.parse (url));
    }

    /**
     * 设置促销是否可见
     *
     * @param state
     *         0 - 可见; 1 - 不可见
     */
    public void setRightState (String state) {
        if (state.equals ("0")) {
            ivTopRight.setVisibility (VISIBLE);
        } else {
            ivTopRight.setVisibility (GONE);
        }
    }

    /**
     * 设置推荐是否可见
     *
     * @param state
     *         0 - 可见; 1 - 不可见
     */
    public void setLeftState (String state) {
        if (state.equals ("0")) {
            ivTopLeft.setVisibility (VISIBLE);
        } else {
            ivTopLeft.setVisibility (GONE);
        }
    }

    public void setMain (int resId) {
        ivMain.setImageResource (resId);
    }

    public void setTopLeft (int resId) {
        ivTopLeft.setImageResource (resId);
    }

    public void setTopRight (int resId) {
        ivTopRight.setImageResource (resId);
    }
}